import request from '@/utils/request'
import type { ItemInfo } from './item'
import type { WarehouseInfo } from './warehouse'

// 库存信息
export interface InventoryInfo {
  inventoryId: number
  itemId: number
  item: ItemInfo
  warehouseId: number
  warehouse: WarehouseInfo
  quantity: number
  unitPrice: number
  totalValue: number
  lastCheckTime: string | null
  createdAt: string
  updatedAt: string
}

// 查询参数
export interface InventoryListParams {
  page?: number
  pageSize?: number
  itemId?: number
  warehouseId?: number
  lowStock?: boolean
}

// 返回结构
export interface InventoryListResponse {
  list: InventoryInfo[]
  total: number
  page: number
  pageSize: number
}

// 获取库存列表
export function getInventoryList(params: InventoryListParams) {
  return request({
    url: '/getInventory',
    method: 'get',
    params
  })
}

// 获取单个物品库存
export function getInventoryById(id: number) {
  return request({
    url: `/inventoryById/${id}`,
    method: 'get'
  })
}

// 库存盘点
export function checkInventory(id: number, data: {
  quantity: number
  notes: string
}) {
  return request({
    url: `/inventory/${id}/check`,
    method: 'put',
    data
  })
}

// 库存统计数据接口
export interface InventoryStats {
  totalItems: number
  totalWarehouses: number
  totalQuantity: number
  totalValue: number
  lowStockItems: number
  highStockItems: number
}

// 获取库存统计数据
export function getInventoryStats() {
  return request({
    url: '/inventory/stats',
    method: 'get'
  })
}

// 库存预警等级
export type WarningLevel = 'low' | 'normal' | 'high'

// 库存预警信息
export interface InventoryWarning {
  inventoryId: number
  itemId: number
  item: {
    itemId: number
    itemName: string
    minimumStock: number
    maximumStock: number
  }
  warehouseId: number
  warehouse: {
    warehouseId: number
    warehouseName: string
  }
  quantity: number
  warningLevel: WarningLevel
}

// 获取库存预警列表
export function getInventoryWarnings() {
  return request({
    url: '/inventory/warnings',
    method: 'get'
  })
}